﻿/*
 *  PAPER    ON   ERVIS  NPAPER ISION  PE  IS ON  PERVI IO  APER  SI  PA
 *  AP  VI  ONPA  RV  IO PA     SI  PA ER  SI NP PE     ON AP  VI ION AP
 *  PERVI  ON  PE VISIO  APER   IONPA  RV  IO PA  RVIS  NP PE  IS ONPAPE
 *  ER     NPAPER IS     PE     ON  PE  ISIO  AP     IO PA ER  SI NP PER
 *  RV     PA  RV SI     ERVISI NP  ER   IO   PE VISIO  AP  VISI  PA  RV3D
 *  ______________________________________________________________________
 *  papervision3d.org • blog.papervision3d.org • osflash.org/papervision3d
 */

/*
 * Copyright 2006 (c) Carlos Ulloa Matesanz, noventaynueve.com.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

// ______________________________________________________________________
//                                                                 Face3D

package org.papervision3d.core.geom
{
import flash.display.*;
import flash.geom.Matrix;
import flash.utils.Dictionary;

import org.papervision3d.Papervision3D;
import org.papervision3d.core.*;
import org.papervision3d.core.proto.*;
import org.papervision3d.objects.DisplayObject3D;

/**
* The Face3D class lets you render linear textured triangles. It also supports solid colour fill and hairline outlines.
*
*/
public class Face3D
{
	/**
	* An array of Vertex3D objects for the three vertices of the triangle.
	*/
	public var vertices :Array;


	/**
	* A material id TODO
	*/
	public var materialName :String;


	/**
	* A MaterialObject3D object that contains the material properties of the back of a single sided triangle.
	*/
//	public var materialBack :MaterialObject3D;


	/**
	* An array of {x,y} objects for the corresponding UV pixel coordinates of each triangle vertex.
	*/
	public var uv :Array;

	// ______________________________________________________________________

	/**
	* [read-only] The average depth (z coordinate) of the transformed triangle. Also known as the distance from the camera. Used internally for z-sorting.
	*/
	public var screenZ :Number;

	/**
	* [read-only] A Boolean value that indicates that the face is visible, i.e. it's vertices are in front of the camera.
	*/
	public var visible :Boolean;


	/**
	* The object where the face belongs.
	*/
//	public var object :Mesh3D;


	/**
	* [read-only] Unique id of this instance.
	*/
	public var id :Number;
	
	/**
	 * Used to store references to the vertices.
	 */
	public var v0:Vertex3D;
	public var v1:Vertex3D;
	public var v2:Vertex3D;
	
	public var gv0:Vertex3D;
	public var gv1:Vertex3D;
	public var gv2:Vertex3D;
	
	/**
	 * The face normal
	 */
	public var faceNormal:Number3D;
	public var faceGNormal:Number3D;
	
	/**
	 * The transformed Face3DInstance
	 */
	public var face3DInstance:Face3DInstance;
	private var _center:Number3D;
	public var shading:Number;
	
	
	/**
	* The Face3D constructor lets you create linear textured or solid colour triangles.
	*
	* @param	vertices	An array of Vertex3D objects for the three vertices of the triangle.
	* @param	material	A MaterialObject3D object that contains the material properties of the triangle.
	* @param	uv			An array of {x,y} objects for the corresponding UV pixel coordinates of each triangle vertex.
	*/
	public function Face3D( vertices:Array, materialName:String=null, uv:Array=null )
	{
		// Vertices
		this.vertices = vertices;
		v0 = vertices[0];
		v1 = vertices[1];
		v2 = vertices[2];
		
		
		// Material
		this.materialName = materialName;
		this.uv = uv;
		this.id = _totalFaces++;
		face3DInstance = new Face3DInstance(this);
		//createNormal();
		
		update(face3DInstance.instance);
		shading = 0;
	}
	
	public function get center():Number3D{
		return _center;
	}
	
	public function update(inst:DisplayObject3D = null){
		
		if(face3DInstance.instance == null){
			gv0 = v0.clone();
			gv1 = v1.clone();
			gv2 = v2.clone();
			
		}else{
			var m0:Matrix3D = Matrix3D.IDENTITY;
			var m1:Matrix3D = Matrix3D.IDENTITY;
			var m2:Matrix3D = Matrix3D.IDENTITY;
			
			m0.n14 = v0.x;
			m0.n24 = v0.y;
			m0.n34 = v0.z;
			m1.n14 = v1.x;
			m1.n24 = v1.y;
			m1.n34 = v1.z;
			m2.n14 = v2.x;
			m2.n24 = v2.y;
			m2.n34 = v2.z;
			
			m0.calculateMultiply( face3DInstance.instance.world, m0 );
			m1.calculateMultiply(  face3DInstance.instance.world, m1 );
			m2.calculateMultiply(  face3DInstance.instance.world, m2 );
			
			gv0.x = m0.n14;
			gv0.y = m0.n24;
			gv0.z = m0.n34;
			gv1.x = m1.n14;
			gv1.y = m1.n24;
			gv1.z = m1.n34;
			gv2.x = m2.n14;
			gv2.y = m2.n24;
			gv2.z = m2.n34;
			
			
		}
		
		_center = new Number3D((gv0.x+gv1.x+gv2.x)/3,(gv0.y+gv1.y+gv2.y)/3,(gv0.z+gv1.z+gv2.z)/3);
		createNormal();
		createGNormal();
	}
	
	public function hitsLine(p1:Number3D, vect:Number3D):int{
		
		var	dotprod:Number = Number3D.dot(faceGNormal, vect);
		//if 0 they are parallel
		if(dotprod < 0)
			return 0;

		var t:Number = -(faceGNormal.x*(p1.x-gv0.x)+faceGNormal.y*(p1.y-gv0.y)+faceGNormal.z*(p1.z-gv0.z))/
             (faceGNormal.x*vect.x+faceGNormal.y*vect.y+faceGNormal.z*vect.z);
		
		if(t < 0) return 0;

		var i:Number3D = new Number3D();
		i.x = p1.x + vect.x*t;
		i.y = p1.y + vect.y*t;
		i.z = p1.z + vect.z*t;

		if(checkFaceDir(gv0, gv1, i))
		  {
			 if(checkFaceDir(gv1, gv2, i))
			 {
				if(checkFaceDir(gv2, gv0, i))
				{
				   // answer in pt_int is insde triangle
				   return 1;
				}
			 }
		  }
		/* var d:Number = distOff(i);
		 var dd:Number = (d/60);
		 if(dd < 6){
		 	//trace(d, dd, (dd)/18);
			return (dd)/18;
		 }*/
		
		return 0;
	}
	
	function distOff(icept){
		
		var dx:Number = icept.x-_center.x;
		var dy:Number = icept.y - _center.y;
		var dz:Number = icept.z - _center.z;
		return Math.sqrt(dx*dx+dy*dy+dz*dz);
		
	}
	
	function checkFaceDir(pt1:Vertex3D, pt2:Vertex3D, pt3:Number3D):Boolean{
	   var testi:Number = (((pt2.y - pt1.y)*(pt3.z - pt1.z)) - ((pt3.y - pt1.y)*(pt2.z - pt1.z)));
	   var testj:Number = (((pt2.z - pt1.z)*(pt3.x - pt1.x)) - ((pt3.z - pt1.z)*(pt2.x - pt1.x)));
	   var testk:Number = (((pt2.x - pt1.x)*(pt3.y - pt1.y)) - ((pt3.x - pt1.x)*(pt2.y - pt1.y)));
	   var dotprod:Number = testi*faceGNormal.x + testj*faceGNormal.y + testk*faceGNormal.z;
	   if(dotprod < 0)
	 	  return false;
	   return true;

	}


	
	// ______________________________________________________________________________
	//                                                                         RENDER
	// RRRRR  EEEEEE NN  NN DDDDD  EEEEEE RRRRR
	// RR  RR EE     NNN NN DD  DD EE     RR  RR
	// RRRRR  EEEE   NNNNNN DD  DD EEEE   RRRRR
	// RR  RR EE     NN NNN DD  DD EE     RR  RR
	// RR  RR EEEEEE NN  NN DDDDD  EEEEEE RR  RR

	/**
	* Draws the triangle into its MovieClip container.
	*
	* @param	container	The default MovieClip that you draw into when rendering.
	* @param	randomFill		A Boolean value that indicates whether random coloring is enabled. Typically used for debug purposes. Defaults to false.
	* @return					The number of triangles drawn. Either one if it is double sided or visible, or zero if it single sided and not visible.
	*
	*/
	public function render( instance:DisplayObject3D, container:Sprite ): Number
	{
		var material:MaterialObject3D = ( this.materialName && instance.materials )? instance.materials.materialsByName[ this.materialName ] : instance.material;
		return material.drawFace3D(instance, this, container.graphics, v0.vertex2DInstance, v1.vertex2DInstance, v2.vertex2DInstance);
	}
	
	public function createNormal():void
	{
		var vn0:Number3D = v0.toNumber3D();
		var vn1:Number3D = v1.toNumber3D();
		var vn2:Number3D = v2.toNumber3D();
		var vt1:Number3D = Number3D.sub(vn1,vn0);
		var vt2:Number3D = Number3D.sub(vn2,vn0);
		
		faceNormal = Number3D.cross(vt2,vt1);
		faceNormal.normalize();
	}
	
	public function createGNormal():void
	{
		var vn0:Number3D = gv0.toNumber3D();
		var vn1:Number3D = gv1.toNumber3D();
		var vn2:Number3D = gv2.toNumber3D();
		var vt1:Number3D = Number3D.sub(vn1,vn0);
		var vt2:Number3D = Number3D.sub(vn2,vn0);
		
		faceGNormal = Number3D.cross(vt2,vt1);
		faceGNormal.normalize();
	}
	
	// ______________________________________________________________________________
	//                                                                        PRIVATE

	private static var _totalFaces:Number = 0;
	
}
}